/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package application;

import controleur.ConnexionBD;
import java.io.FileInputStream;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 *
 * @author James
 */
public class ImageDAO {
    private static PreparedStatement pstmt;
    private static ResultSet results;

    public static ResultSet getImage(final String id) {
        final String query = "select * from image where id_image=?";

        try {
            pstmt = ConnexionBD.creerConnexion().prepareStatement(query);
            pstmt.setInt(1, Integer.parseInt(id));
            results = pstmt.executeQuery();

        } catch (Exception e) {
            return null;
        }
        return results;
    }
    
    public static int updateImage(final FileInputStream fin, final String idImage) {
        final String query = "update image set "
                + "image=? "
                + "where id_image=?";
        
        int rowResults;

        try {
            pstmt = ConnexionBD.creerConnexion().prepareStatement(query);
            pstmt.setBlob(1, fin);
            pstmt.setInt(2, Integer.parseInt(idImage));
            rowResults = pstmt.executeUpdate();
        } catch (Exception e) {
            rowResults = 0;
        }
        return rowResults;
    }
}
